package cn.maxhou.util.fastjson2;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.PropertyPreFilter;

import java.util.Set;

public class FastJson2ensitivePropFilter implements PropertyPreFilter {

    /**通过构造方法注入需要排除的属性集合*/
    public FastJson2ensitivePropFilter(Set<String> excludes){
        this.excludes = excludes;
    }
    private Set<String> excludes;

    /**处理字段,返回ture表示字段需要序列化,返回false表示不需要*/
    @Override
    public boolean process(JSONWriter jsonWriter, Object obj, String propertyName) {
        if(excludes==null){
            //没有排除属性,直接允许
            return true;
        }
        //不包括在excludes中的属性才需要序列化
        boolean isNeed=!excludes.contains(propertyName);
        return isNeed;
    }
}
